package com.newer.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newer.dao.sys.mapper.ResourceMapper;
import com.newer.dao.sys.mapper.RoleMapper;
import com.newer.dao.sys.mapper.RoleResourceMapper;
import com.newer.pojo.sys.Resource;
import com.newer.pojo.sys.ResourceExample;
import com.newer.pojo.sys.Role;
import com.newer.pojo.sys.RoleExample;
import com.newer.pojo.sys.RoleExample.Criteria;
import com.newer.pojo.sys.RoleResource;
import com.newer.service.sys.iter.RoleService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.Conditions;
import com.newer.utils.sys.ConvertString;
import com.newer.utils.sys.IsNotNullUtil;
/**
 * 业务层
 * @author Administrator
 *
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService{
	
	/**
	 * 载入持久层
	 */
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private ResourceMapper resourceMapper;
	@Autowired
	private RoleResourceMapper roleResourceMapper;
	/**
	 * 查询状态为零的
	 */
	@Override
	public BootstrapDataTable selectRoleByExample(BootstrapDataTable dataTable,Conditions conditions) {
		// TODO Auto-generated method stub
		System.out.println("查询角色"+dataTable);
		// 给PageHelper设参数
		PageHelper.offsetPage(dataTable.getOffset(), dataTable.getLimit());
		// 实例化user条件对象
		RoleExample roleExample = new RoleExample();
		//设置查询对象
		Criteria createCriteria = roleExample.createCriteria();
		//设置And or 查询条件
		Criteria createCriteria2 = roleExample.createCriteria();
		// 去重
		roleExample.setDistinct(true);
		//设置查询条件
		if(IsNotNullUtil.isNotNull(conditions)) {
			if(IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("roleName")) {
				createCriteria.andRoleNameLike("%"+conditions.getContent()+"%");
			}
			if(IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("roleCode")) {
				createCriteria.andRoleCodeLike("%"+conditions.getContent()+"%");
			}
			if(IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("NameOrCode")) {
				createCriteria.andRoleCodeLike("%"+conditions.getContent()+"%").andDeleteStatusEqualTo(0);
			}
			if(IsNotNullUtil.isNotNull(conditions.getType()) && conditions.getType().equals("NameOrCode")) {
				createCriteria2.andRoleNameLike("%"+conditions.getContent()+"%").andDeleteStatusEqualTo(0);
			}
		}
		//设值
		roleExample.or(createCriteria2);
		//设置排序字段的不一致
		if (IsNotNullUtil.isNotNull(dataTable.getSort())&&IsNotNullUtil.isNotNull(dataTable.getOrder())) {
			String convertStr = ConvertString.convertStr(dataTable.getSort());
			//设置排序字段
			roleExample.setOrderByClause(convertStr+" "+dataTable.getOrder());
		}
		//查询k角色列表
		List<Role> roleList = roleMapper.selectByExample(roleExample);
		// 实例一个PageInfo对象 ，并设置参数roleList
		PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
		//输出角色分页数据
		for (Role role : roleList) {
			System.out.println(role);
		}
		// 实例一个数据表对象
		BootstrapDataTable bootDataTable = new BootstrapDataTable();
		//设置总记录数
		bootDataTable.setTotal((int)pageInfo.getTotal());
		//设置每页显示数据
		bootDataTable.setRows(pageInfo.getList());
		//返回数据
		return bootDataTable;
	}
	/**
	 *检查角色名是否存在 
	 */
	@Override
	public Map<String, Boolean> selectRoleByRoelName(String roleName,Integer id) {
		// TODO Auto-generated method stub
		System.out.println("角色名"+roleName);
		//实例化map
		Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
		// 设置结果值
		boolean result = false;
		//如果查询到与自己想同照样通过
		Role selectByPrimaryKey = roleMapper.selectByPrimaryKey(id);
		if(IsNotNullUtil.isNotNull(id)){
			if(selectByPrimaryKey.getRoleName().equals(roleName)) {
				result = true;
				resultMap.put("valid", result);
				return resultMap;
			}
		}
		//查询角色
		Role selectByRoelName = roleMapper.selectByRoelName(roleName);
		//判断对象是否为空      查到返回false否则true
		if(selectByRoelName!=null) {
			result = false;//查到返回false
		}else {
			result = true;
		}
		//设置返回结果
		resultMap.put("valid", result);
		//返回结果
		return resultMap;
	}
	/**
	 * 添加角色
	 */
	@Override
	public int insertRole(Role role) {
		// TODO Auto-generated method stub
		// 设删除状态为0, 1:删除状态 ,0:未删除状态
		role.setDeleteStatus(0);
		//打印页面传值对象
		System.out.println(role);
		// 调用mapper层方法插入角色对象
		return roleMapper.insertSelective(role);
	}
	/**
	 * 根据id查询角色
	 */
	@Override
	public Role selectRoleById(Integer id) {
		// TODO Auto-generated method stub
		System.out.println("更新角色："+id);
		//根据id查询角色
		Role selectByPrimaryKey = roleMapper.selectByPrimaryKey(id);
		System.out.println("要更行的角色:"+selectByPrimaryKey);
		return selectByPrimaryKey;
	}
	/**
	 * 修改角色
	 */
	@Override
	public int updateRoleByPrimaryKey(Role record) {
		// TODO Auto-generated method stub
		System.out.println("修改角色："+record);	
		//调用底层修改方法
		return roleMapper.updateByPrimaryKeySelective(record);
	}
	/**
	 * 假删除
	 */
	@Override
	public int shamRoleById(Integer id) {
		//实例化role对象
		Role role = new Role();
		//设置id
		role.setId(id);
		//设置删除状态
		role.setDeleteStatus(1);
		//删除关联表
		roleResourceMapper.deleteResourceByRoleId(id);
		//调用底层删除方法
		return roleMapper.updateByPrimaryKeySelective(role);
	}
	/**
	 * 批量删除角色
	 */
	@Override
	public int deleteRoleByIds(String Ids) {
		//截取
        String[] split = Ids.split(",");
 		//实例化集合
 		List<String> ids = null;
 		//添加删除id
         if(IsNotNullUtil.isNotNull(split)){
        	 ids=new ArrayList<>();
             for (String string : split) {
     			ids.add(string);
     		} 
         }
         //删除关联表
         roleResourceMapper.deleteResourceByRoleIds(ids);
		//调用底层删除
		return roleMapper.deleteByIds(ids);
	}
	/**
	 * 根据角色id查询权限
	 */
	@Override
	public List<RoleResource> selectAuthorByRoleId(Integer roleId) {
		System.out.println(roleId);
		return roleResourceMapper.selectAuthorByRoleId(roleId);
	}
	/**
	 * 查询资源树
	 */
	@Override
	public List<Resource> ResourceTree() {
		return resourceMapper.selectByExample(null);
	}
	/**
	 * 授权
	 */
	@Override
	public void insertRoleResource(Integer roleId, String rids) {
		System.out.println("rids"+rids);
		try {
		//先删除角色之前的权限
		roleResourceMapper.deleteResourceByRoleId(roleId);
		//实例化一个对象集合
		List<RoleResource> roleResource = new ArrayList<RoleResource>();
		if(IsNotNullUtil.isNotNull(rids)) {
			// 分隔字符串，遍历字符串数组
			for (String resourceIds : rids.split(",")) {
				//实例化对象
				RoleResource roleRe = new RoleResource();
				roleRe.setRoleId(roleId);//设置角色id
				roleRe.setResourceId(Integer.parseInt(resourceIds));//设置资源id
				roleResource.add(roleRe);//添加至集合
			}
			System.out.println(roleResource+":roleResource");
			roleResourceMapper.insertRoleResource(roleResource);
		}
		}catch(Exception e) {
			System.out.println(e.getMessage());
		  }
		}
	
}
